package com.rceeslca.modules.cam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rceeslca.modules.cam.dao.CamUserDao;
import com.rceeslca.modules.cam.entity.CamMajor;
import com.rceeslca.modules.cam.service.CamUserService;
import com.rceeslca.modules.cam.vo.CamUserVo;
import com.rceeslca.modules.sys.dao.SysRoleDao;
import com.rceeslca.modules.sys.dto.SysUserDTO;
import com.rceeslca.modules.sys.entity.SysRoleEntity;
import com.rceeslca.modules.sys.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户
 */
@Service
@AllArgsConstructor
public class CamUserServiceImpl implements CamUserService {

    @Autowired
    private CamUserDao camUserDao;

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysUserService sysUserService;


    /**
     * 分页查询
     * @param page 分页
     * @param param 参数
     */
    @Override
    public IPage<CamUserVo> pageList(Page<CamUserVo> page, CamUserVo param) {
        return camUserDao.pageSelect(page,param.getRoleName(),param.getUserName(),param.getMajorId(),param.getRealName());
    }


    /**
     * 列展示
     * @param param 参数
     */
    @Override
    public List<CamUserVo> listUser(CamUserVo param) {
        return camUserDao.listUser(param.getRoleName(),null);
    }


    /**
     * 批量插入
     * @param userDtoList 用户
     */
    @Override
    public void batchAdd(List<SysUserDTO> userDtoList) {

        QueryWrapper<SysRoleEntity> roleWrapper = new QueryWrapper<>();
        roleWrapper.lambda().eq(SysRoleEntity::getName,"学生");
        List<SysRoleEntity> sysRoleList = sysRoleDao.selectList(roleWrapper);

        if(sysRoleList.size()==1){
            SysRoleEntity studentRole = sysRoleList.get(0);
            List<Long> roleIdList = new ArrayList<>();
            roleIdList.add(studentRole.getId());

            for(SysUserDTO sysUserDTO : userDtoList){
                sysUserDTO.setStatus(1);
                sysUserDTO.setSuperAdmin(0);
                sysUserDTO.setRoleIdList(roleIdList);
                sysUserService.save(sysUserDTO);
            }
        }
    }

}
